import sa from '../function/global'
export function getStorage(key, callback) {
  try {
    sa.platform_obj.getStorage({
      key: key,
      complete: callback
    })
  } catch (e) {
    sa.log('获取 storage 失败！')
  }
}

export function getStorageSync(key) {
  var store = ''

  try {
    store = sa.platform_obj.getStorageSync(key)
  } catch (e) {
    try {
      store = sa.platform_obj.getStorageSync(key)
    } catch (e2) {
      sa.log('获取 storage 失败！')
    }
  }
  // if (isJSONString(store)) {
  //   store = JSON.parse(store);
  // }
  return store
}

export function setStorageSync(key, value) {
  var item
  try {
    item = JSON.stringify(value)
  } catch (error) {
    sa.log('序列化缓存对象失败！')
  }
  var fn = function () {
    sa.platform_obj.setStorageSync(key, item)
  }
  try {
    fn()
  } catch (e) {
    sa.log('set Storage fail --', e)
    try {
      fn()
    } catch (e2) {
      sa.log('set Storage fail again --', e2)
    }
  }
}
